import Request from 'utils/request';

import AdvertisementForm from 'models/advertisement/advertisementForm';

export default class AdvertisementApi {
  static getAdvertisementList(search, pagination) {
    const apiName = 'api/advertisement/list';
    let postData = search.getObject(pagination);
    return Request.post(apiName, postData).then(res => {
      let advertisementList = res.data;
      advertisementList = _.map(advertisementList, advertisement => {
        advertisement = new AdvertisementForm(advertisement);
        return advertisement;
      });

      pagination.totalCount = res.rows;
      return advertisementList;
    });
  }

  static addAdvertisement(advertisementForm) {
    const apiName = 'api/advertisement/save';
    let postData = advertisementForm.getObject();

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static updateAdvertisement(advertisementForm) {
    const apiName = 'api/advertisement/update';
    let postData = advertisementForm.getObject();

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static delAdvertisement(id) {
    let apiName = 'api/advertisement/deleteIds';
    return Request.post(apiName, [id]).then(res => {
      return res.msg;
    });
  }
}
